﻿using System;

namespace FirstFloor.Reversi
{
    /// <summary>
    /// Represents a board piece
    /// </summary>
    public class Piece
        : Location
    {
        private PieceColor color = PieceColor.None;
        private bool marked = false;
        private bool selectable = false;

        /// <summary>
        /// Initializes a new instance of the <see cref="Piece"/> class.
        /// </summary>
        public Piece(int x, int y)
            : base(x, y)
        {
        }

        /// <summary>
        /// Gets or sets the color.
        /// </summary>
        /// <value>The color.</value>
        public PieceColor Color
        {
            get { return this.color; }
            set { this.color = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Piece"/> is marked.
        /// </summary>
        /// <value><c>true</c> if marked; otherwise, <c>false</c>.</value>
        public bool Marked
        {
            get { return this.marked; }
            set { this.marked = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="Piece"/> is selectable.
        /// </summary>
        /// <value><c>true</c> if selectable; otherwise, <c>false</c>.</value>
        public bool Selectable
        {
            get { return this.selectable; }
            set { this.selectable = value; }
        }

        /// <summary>
        /// Gets the name of the image element.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        /// <returns></returns>
        public string GetImageElementName()
        {
            return string.Format("img{0}{1}", X, Y);
        }

        /// <summary>
        /// Gets the image URL for the current state of this piece.
        /// </summary>
        /// <returns></returns>
        public Uri GetImageUrl()
        {
            return new Uri(string.Format("Images/{0}{1}.jpg", this.color.ToString().ToLower(), this.marked ? "_marked" : ""), UriKind.Relative);
        }

        /// <summary>
        /// Gets the name of the location from image.
        /// </summary>
        /// <param name="imageName">Name of the image.</param>
        /// <returns></returns>
        public static Location GetLocationFromImageName(string imageName)
        {
            int x = (int)imageName[3] - (int)'0';
            int y = (int)imageName[4] - (int)'0';

            return new Location(x, y);
        }
    }
}
